//
//  BaseViewController.m
//  TinyFortune
//
//  Created by lc on 16/5/15.
//  Copyright © 2016年 lc. All rights reserved.
//

#import "BaseViewController.h"
#import "AVFile +  getDictionary.h"
@interface BaseViewController (){
    UITextField *_search;
}

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self judgeSingle];

}
#pragma mark -单身才加载的view
- (void)judgeSingle{
    //单身的话才加载
    if (!_isSingle) {
        [self createCommonPart];
    }
}
#pragma mark -createCommonPart
- (void)createCommonPart{
    
    //顶部背景
    _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/4.0f)];
    _topView.backgroundColor = [UIColor colorWithRed:0/255.0f green:182/255.0f blue:255/255.0f alpha:1];
    _topView.userInteractionEnabled = YES;
    
    //提示条
    CGFloat noticeMarginY = 30;
    CGFloat noticeMarginX =self.view.frame.size.width/3.0f;
    UILabel *noticeLabel = [[UILabel alloc]initWithFrame:CGRectMake(noticeMarginX * 0.7, noticeMarginY, noticeMarginX * 1.5, 20)];
    noticeLabel.backgroundColor = [UIColor whiteColor];
    noticeLabel.text = @"体验全部功能请添加另一半";
    noticeLabel.textColor = [UIColor colorWithRed:0/255.0f green:182/255.0f blue:255/255.0f alpha:1];
    noticeLabel.textAlignment = NSTextAlignmentCenter;
    [noticeLabel setFont:[UIFont systemFontOfSize:14]];
    [_topView addSubview:noticeLabel];
    
    
    //搜索条
    //搜索条间距
    CGFloat marginSearch = 80;
    _search = [[UITextField alloc]initWithFrame:CGRectMake(marginSearch,_topView.frame.size.height/2.5f , _topView.frame.size.width-marginSearch*2.0f, _topView.frame.size.height/5.0f)];
    
    [_search.layer setCornerRadius:5];
    _search.backgroundColor = [UIColor whiteColor];
    _search.placeholder = @"输入另一半恩爱号";
    _search.clearsOnBeginEditing = YES;
    
    [_topView addSubview:_search];
    //搜索条左边
    //搜索条右边
    UIButton *searchButton = [[UIButton alloc]initWithFrame:CGRectMake(_search.frame.origin.x + _search.frame.size.width, _search.frame.origin.y, marginSearch/1.5, _search.frame.size.height)];
    
    searchButton.backgroundColor = [UIColor colorWithRed:86/255.0f green:202/255.0f blue:255/255.0f alpha:0.5];
    [searchButton setTitle:@"查找" forState:UIControlStateNormal];
        [searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [searchButton.layer setCornerRadius:4];
    searchButton.layer.masksToBounds = YES;
    [searchButton addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchDown];
    [_topView addSubview:searchButton];
    //恩爱号
    _idNubmer = [[AVUser currentUser].mobilePhoneNumber integerValue];
    

    UILabel *numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(1.4 * marginSearch, _topView.frame.size.height/1.25, _topView.frame.size.width/2.3f, 15)];
    [numberLabel setText:[NSString stringWithFormat:@"我的恩爱号%lu",(long)_idNubmer]];
    
    
    [_topView addSubview:numberLabel];
    //numberLabel.backgroundColor = [UIColor redColor];
    //复制按钮
    UIButton *copyButton = [[UIButton alloc]initWithFrame:CGRectMake(1.5 * marginSearch + numberLabel.frame.size.width, _topView.frame.size.height/1.3,_topView.frame.size.height/3.3 , 25)];
    [copyButton setTitle:@"复制" forState:UIControlStateNormal];
    [copyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    copyButton.backgroundColor = [UIColor colorWithRed:51/255.0f green:47/255.0f blue:49/255.0f alpha:1];
    [copyButton.layer setCornerRadius:4];
    copyButton.layer.masksToBounds = YES;
    [copyButton addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchDown];
    [_topView addSubview:copyButton];
    
    // _isSingle = YES;
    [self.view addSubview:_topView];
    
    
}

#pragma mark - 点击事件
- (void)onClick:(UIBarButtonItem *)item{
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"抱歉" message:@"该功能还未实现" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
    [alert show];
    NSLog(@"我被点了");
}
//MARK:查找另一半的功能
- (void)search{
    NSLog(@"1");
    AVUser *user = [AVUser currentUser];
    //根据查询来推送消息
   AVQuery *query = [AVUser query];
    [query whereKey:@"mobilePhoneNumber" equalTo:_search.text];
    NSLog(@"%@",query);
    AVPush *push = [AVPush new];
    [push setQuery:query];
    [push setMessage:@"I want to be your lovers"];
    [push sendPushInBackground];
    //在message表里面新建一个对象
    AVObject *object = [[AVObject alloc]initWithClassName:@"message"];
    //头像
    AVFile *icon = [user objectForKey:@"iconImage"];
    if (icon) {
        NSDictionary *dict = [AVFile fileWithDictionary:icon];
        [object setObject:dict[@"url"] forKey:@"url"];
    }
   //昵称
    [object setObject:user.username forKey:@"requestName"];
    //某某请求添加你为情侣信息
    [object setObject:[NSString stringWithFormat:@"%@请求做你的情侣",user.username] forKey:@"desc"];
    //目的id(该ID用来存储电话号码。。。)
    [object setObject:_search.text forKey:@"ID"];
    
    //暂且先设置四个字段
    
    [object saveInBackground];

}
@end
